home *** CD-ROM | disk | FTP | other *** search
-
- package sub_arctic.lib;
-
- import sub_arctic.output.*;
- import sub_arctic.input.*;
- import sub_arctic.constraints.std_function;
-
- import java.awt.Font;
- import java.util.Vector;
-
- /**
- * This object builds a collection of labeled toggle switches. This
- * useful when you want to create a set of exclusive or non-exclusive
- * choices from a set of strings. <p>
- *
- * The reason the 0th child is special is that all other children get
- * added to his group. Groups are circularly linked lists of toggles
- * who need to be exclusive of each other. The functions which manipulate
- * "groups" with respect to toggles are defined in the toggle class.
- *
- * @see toggle
- * @author Ian Smith
- */
-
- public class text_toggle_collection extends base_parent_interactor {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * vector of the strings for this collection
- */
- protected Vector _data;
-
- /**
- * Access the vector of strings in this collection.
- * @return Vector the strings in this collection.
- */
- public Vector data() { return _data;};
-
- /**
- * Set the data in this collection to be (and its toggles to display)
- * an array of strings.
- *
- * @param String[] s the array of strings you want the user to choose from.
- */
- public void set_data(String[] s) {
- int i;
-
- /* get rid of previous data */
- clear_items();
- for (i=0; i<s.length; ++i) {
- set_item(i,s[i]);
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The callback object to send callbacks to.
- */
- protected callback_object _callback_obj;
-
- /**
- * Access the object we send callbacks to.
- *
- * @return callback_object the object we send the callbacks to.
- */
- protected callback_object callback_obj() { return _callback_obj;};
-
- /**
- * Modify the current callback object.
- *
- * @param callback_object cb the new object to receive callbacks.
- */
- protected void set_callback_obj(callback_object cb ) { _callback_obj=cb;};
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Get rid of all items
- */
- public void clear_items() {
- _data.removeAllElements();
-
- /* repeat this until no more children */
- while (num_children()!=0) {
- /* remove the first child */
- remove_child(child(0));
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Use group tells the collection if its collection forms one group or
- * not. It is only consulted when NEW items are added to the
- * collection.
- */
- protected boolean _use_group;
-
- /**
- * Is the collection using a group (exclusive choices)?
- * @return boolean true if this collection is a group.
- */
- protected boolean use_group() { return _use_group;};
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Change the state of the use_group variable. Note that it is only
- * consulted as new items are added to the collection.
- *
- * @param boolean b whether or not this collection should use a group or
- * not (if this is true, then choices are exclusive).
- */
- protected void set_use_group(boolean b) { _use_group=b;};
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This controls if you want a fixed width for the labels. If you specify
- * a value other than negative 1, you get that width. It is only
- * considered when NEW items are added to the collection.
- */
- protected int _fixed_width=-1;
-
- /**
- * Get the width of all items in this collection.
- *
- * @return int the width all items in the collection or -1 if not
- * currently set.
- */
- protected int fixed_width() { return _fixed_width;};
-
- /**
- * Set the value of the width of all items in the collection. Note that
- * this value is only consulted when new items are added to the
- * collection.
- * @param int f the new fixed width for this collection (use -1 for no fixed
- * width)
- */
- protected void set_fixed_width(int f) { _fixed_width=f;};
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Make a toggle. Its useful to override this if you want a different
- * toggle class.
- *
- * @param int i which toggle (which possible) you want this new toggle to be
- * @return toggle the toggle we want added to the collection
- */
- protected toggle make_toggle(int i)
- {
- label_toggle t,tmp;
-
- if (use_group()) {
- t=new label_toggle(true,callback_obj(), fixed_width(),font(),
- (String)_data.elementAt(i));
- } else {
- t=new label_toggle(false,callback_obj(), fixed_width(),font(),
- (String)_data.elementAt(i));
- }
-
- t.set_user_info(new Integer(i));
-
- /* make a group? */
- if (use_group()) {
- /* we use #0 as the group leader, so he is not in a group */
- if (i!=0) {
- t.add_to_group_of(nth_toggle(0));
- }
- }
-
- return t;
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Set the ith item in the collection to display a certain string.
- * You better make sure the items are contiguous or you'll get a
- * null pointer exception when you try to redraw this object.<p>
- *
- * Items begin numbering at zero.
- *
- * @param int i the number of the item you want to change.
- * @param String s the new string to display.
- */
- public void set_item(int i,String s) {
- label_toggle t;
- /* put in the data list */
- if (_data.size()==i) {
-
- // simple case is add at end
- _data.addElement(s);
-
- /* make the object */
- t=(label_toggle)make_toggle(i);
-
- /* add child */
- add_child(t);
-
- } else {
-
- // better hope its bigger
- _data.setElementAt(s,i);
- t=(label_toggle) child(i);
-
- /* set the text to be right */
- t.set_text(s);
- }
-
- /* snap on constraints */
- set_toggle_constraints(t,i);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Put the constraints on for the the toggle.
- *
- * @param toggle t the toggle we are constraining.
- * @param int i the position in the toggle of this collection.
- */
- protected void set_toggle_constraints(toggle t,int i)
- {
-
- /* if first obj, then put it off the parent */
- if (i==0) {
- t.set_y_constraint(std_function.offset(PARENT.Y(), default_offset()));
- } else {
- /* space from previous child */
- t.set_y_constraint(std_function.offset(PREV_SIBLING.Y2(), default_offset()));
- }
-
- /* y is zero plus the offset in all cases */
- t.set_x_constraint(std_function.offset(PARENT.X(), default_offset()));
-
- /* these things are sized internally on w and h */
- }
-
- //had:
- //* @exception general PROPAGATED.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * How much space around the edges and between the items.
- */
- protected static int _default_offset=2;
-
- /**
- * Return the default distance around the edges of items.
- * @return int the amount of space around items
- */
- protected static int default_offset() { return _default_offset;};
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Build a new text/toggle collection object. This assumes that you
- * will position this object with constraints. If you want the objects
- * formed into a group, set use_a_group. If you want all the labels to
- * be the same width, set fixed_width to the width; if you don't want
- * fixed with, pass -1. The callback object is called when an object
- * get selected. You'll get a user info value which has an Integer in
- * it. That is the index number of the object just selected.
- *
- * @param String[] s the initial set of strings to display.
- * @param boolean use_a_group pass true here if you want the set of
- * toggles to be exclusive, pass false if
- * you want them to be non-exclusive.
- * @param int fw the 'forced width' of the objects in
- * the collection in pixels (if you use
- * -1 the objects don't get forced to
- * particular width).
- * @param callback_object cb the object you want callbacks
- * delivered to.
- */
- public text_toggle_collection(String[] s, boolean use_a_group,
- int fw, callback_object cb)
- {
- super(0,0);
-
- _data=new Vector(10);
- set_use_group(use_a_group);
- set_fixed_width(fw);
- set_callback_obj(cb);
- set_data(s);
-
- /* just make us get around our children */
- set_w_constraint(std_function.offset(MAX_CHILD.X2(), default_offset()));
- set_h_constraint(std_function.offset(MAX_CHILD.Y2(), default_offset()));
- }
-
- //had
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Indexing from 0, get the toggle associated with that index.
- * @param int i the index in question
- * @return toggle the interactor (toggle) for a particular index
- */
- public toggle nth_toggle(int n)
- {
- return (toggle)child(n);
- }
- //had
- //* @exception general PROPAGATED
-
- /**
- * Get the String value for that an index.
- * @param int i the index in question
- * @return String the string associated with the index
- */
- public String nth_string(int n) {
- return (String)_data.elementAt(n);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This is where the font lives for drawing the labels.
- */
- protected Font _font;
-
- /**
- * Return the font we are drawing labels in.
- * @return Font the font for the strings displayed in this object.
- */
- public Font font() { return _font;}
-
- /**
- * Set the font to use for this collection.
- * @param Font f the new font to draw the labels in
- */
- public void set_font(Font f) {
- String d[];
- int i;
-
- _font=f;
- d=new String[_data.size()];
- for(i=0; i<_data.size(); ++i) {
- d[i]=nth_string(i);
- }
- set_data(d);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-